package com.xuanyue.db.xuan.core.index;

import java.io.File;
import java.util.List;

import com.xuanyue.db.xuan.core.exception.SQLException;
import com.xuanyue.db.xuan.core.table.IBitIndex;
import com.xuanyue.db.xuan.core.table.IColumn;
import com.xuanyue.db.xuan.core.table.INumberColumn;
import com.xuanyue.db.xuan.core.table.ISortElement;
import com.xuanyue.db.xuan.core.table.expr.Expr;
import com.xuanyue.db.xuan.core.table.expr.ExprValue;
import com.xuanyue.db.xuan.core.table.expr.ExprValueType;
import com.xuanyue.db.xuan.core.table.expr.SortTemplate;
import com.xuanyue.db.xuan.msg.VLAUETYPE;
/**
 * 整形
 * @author 解观海
 * @email  guanhaixie@sina.cn
 * @date 2020年6月23日
 * @version 0.1
 */
public class IntIndex implements INumberColumn<Integer>{
	private NumberIndex data;
	private String path;
	@Override
	public void setPath(String path) {
		this.path = path;
		data.setPath(path);
	}
	public IntIndex(int max) {
		data = new NumberIndex(max);
	}
	@Override
	public boolean modify(IColumn newOne,boolean cpDataFromOld) {
		if(newOne instanceof IntIndex) {
			IntIndex one = (IntIndex)newOne;
			return data.modify(one.data,cpDataFromOld);
		}
		return false;
	}
	
	@Override
	public void save() {
		data.save();
	}
	@Override
	public void load() {
		data.load();
	}
	@Override
	public void init() {
		File p = new File(path);
		if(!p.exists()) {
			p.mkdirs();
		}
		data.init();
	}
	@Override
	public void equeals(IBitIndex cache, Number value) {
		data.equeals(cache,value);
	}

	@Override
	public void greater(IBitIndex cache, IBitIndex now, Number value) {
		data.greater(cache, now, value);
	}

	@Override
	public void less(IBitIndex cache, IBitIndex now, Number value) {
		data.less(cache, now, value);
	}

	@Override
	public void greaterAndEq(IBitIndex cache, IBitIndex now, Number value) {
		data.greaterAndEq(cache, now, value);
	}

	@Override
	public void lessAndEq(IBitIndex cache, IBitIndex now, Number value) {
		data.lessAndEq(cache, now, value);
	}

	@Override
	public void set(int rowId, ExprValue valueO) {

		if(ExprValueType.NULL.is(valueO.getType())) {
			data.set(rowId, valueO);
			return;
		}else if(ExprValueType.INT.is(valueO.getType())) {
			Integer value = valueO.getValue();
			ExprValue in = new ExprValue();
			in.setType(ExprValueType.LONG);
			in.setValue(value.longValue());
			data.set(rowId, in);
		}else {
			throw new SQLException("value type ERROR:"+valueO.getType()+"  is not a int value");
		}
	}

	@Override
	public Integer get(int rowId) {
		// TODO Auto-generated method stub
		Long v = data.get(rowId);
		if(v==null) {
			return null;
		}else {
			return v.intValue();
		}
	}
	@Override
	public ExprValue getExprValue(int rowId) {
		return new ExprValue(ExprValueType.INT, get(rowId));
	}
	
	@Override
	public void flush(int maxId) {
		data.flush(maxId);
	}

	@Override
	public int getDataSize() {
		return data.getDataSize();
	}

	@Override
	public List<ISortElement> getSortE(SortTemplate sortTemplate) {
		return data.getSortE(sortTemplate);
	}
	@Override
	public boolean checkSortE(SortTemplate sortTemplate) {
		return sortTemplate.isType(SortTemplate.GENERAL);
	}
	@Override
	public VLAUETYPE getType() {
		return VLAUETYPE.INT;
	}

	@Override
	public void saveRow(int rowId){
		data.saveRow( rowId);
	}
	@Override
	public boolean checkType(ExprValue v) {
		
		return ExprValueType.INT.is(v.getType())||ExprValueType.NULL.is(v.getType());
	}
	@Override
	public void exe(Expr expr, List<IBitIndex> caches) {
		ExprValue inV = new ExprValue();
		ExprValue ev = expr.getValue();
		if(ExprValueType.INT.is(ev.getType())) {
			inV.setType(ExprValueType.LONG);
			int ov = ev.getValue();
			inV.setValue((long)ov);
		}else if(ExprValueType.NULL.is(ev.getType())){
			ev.setType(ExprValueType.NULL);
		}else {
			throw new SQLException("expr value type not support :"+ ev.getType());
		}
		Expr in = new Expr();
		in.setOp(expr.getOp());
		in.setValue(inV);
		data.exe(expr,caches);
	}
	@Override
	public int checkAndContSource(Expr expr) {
		ExprValue inV = new ExprValue();
		ExprValue ev = expr.getValue();
		if(ExprValueType.INT.is(ev.getType())) {
			inV.setType(ExprValueType.LONG);
			int ov = ev.getValue();
			inV.setValue((long)ov);
		}else if(ExprValueType.NULL.is(ev.getType())){
			ev.setType(ExprValueType.NULL);
		}else {
			return 0;
		}
		Expr in = new Expr();
		in.setOp(expr.getOp());
		in.setValue(inV);
		return data.checkAndContSource(expr);
	}
}
